// pages/staff/staff.js
const App = getApp();
const Api = require('../../utils/api.js');
import regeneratorRuntime from '../../utils/runtime.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 1,
        reported: false,
        staff: false,
        usertype: '',
        currentNickname: '',
        currentUid: '',
        qrname:"",
        qrserious:1,
        qrlink:"",
        text1:"",
        text2:"",
        inviteData:[]
    },

    tabHandle(e) {
        let that = this
        that.setData({
            current: e.currentTarget.dataset.num
        })
    },

    reportedHandle(e) {
        let that = this
        console.log(e.currentTarget.dataset.uid)
        that.setData({
            reported: !that.data.reported,
            currentNickname: e.currentTarget.dataset.nickname,
            currentUid: e.currentTarget.dataset.uid,
        })
    },

    staffHandle() {
        let that = this
        that.setData({
            staff: !that.data.staff
        })
    },

    staffDetail(e) {
        wx.navigateTo({
            url: '/pages/staff_detail/staff_detail?id=' + e.currentTarget.id,
        })
    },

    staffRecord(e) {
        wx.navigateTo({
            url: '/pages/staff_record/staff_record?id=' + e.currentTarget.id + '&name=' + e.currentTarget.dataset.name,
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let _this = this

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let _this = this
        if (!wx.getStorageSync('userInfo')) {
            wx.navigateTo({
                url: '/pages/login/login',
            })
        } else {
            // 0:茶企 1:茶农 2:茶工 3:未确定 
            let usertypeNew = wx.getStorageSync('usertype');
            _this.setData({
                usertype: usertypeNew
            })
            if (usertypeNew == 0 || usertypeNew== 1) _this.getMyWorkers()
            if (usertypeNew == 2){
                _this.getMyCode()
                //_this.checkInvite()
            }
            if (usertypeNew == 3 || usertypeNew == null) {
                
            }

            
        }
    },

    getMyWorkers() {
        return new Promise((resolve) => {
            let _this = this;
            let data = {};
            Api.fetch('getMyWorkers', data)
                .then(res => {
                    console.log(res.data);
                    if (res.data.code == 1) {
                        _this.setData({
                            allow: res.data.data.allow,
                            deny: res.data.data.deny,
                            company: res.data.data.company,
                            baned:res.data.data.baned
                        })
                        resolve(true)
                    }
                })
                .catch(e => {
                    console.log(e);
                })
        })
    },
    //茶工获取自己的健康码
    getMyCode(){
        wx.showLoading({
            title: '数据加载中',
        })
        let _this = this;
        Api.fetch('getMyCode', {})
            .then(res => {
                wx.hideLoading()
                if (res.data.code == 1) {
                    let serious = res.data.data.serious;
                    let qrserious=1;
                    let text1="",text2="";
                    if(serious<4){
                        qrserious ="#4FD393"
                        text1 = "凭借此码可在本市范围内务工"
                        text2 = "请主动出示 配合用工"
                    }else if(serious <6){
                        qrserious = "#F3BF66"
                        text1 = "您需要自动隔离7天，并每天上报体温记录"
                        text2 = "请主动配合 防疫无忧"
                    }else{
                        qrserious = "#D33F46"
                        text1 = "抱歉，您目前无法进入黄山区域务工。"
                        text2 = "请主动配合 防疫无忧"
                    }
                    _this.setData({
                        qrname:res.data.data.name,
                        qrserious:qrserious,
                        text1:text1,
                        text2:text2,
                        qrlink:res.data.data.link
                    });
                    
                }
            })
            .catch(e => {
                console.log(e);
            })
    },
    checkInvite(){
        let _this = this;
        Api.fetch('getMyInvite', {}).then(res=>{
            let data = res.data.data;
            if(data.id){
                wx.showModal({
                    title: '加入邀请',
                    content: data.invitor+'邀请您加入'+data.enterprise,
                    success(res) {
                        if (res.confirm) {
                            Api.fetch('confirmInvite', { inviteId:data.id}).then(res=>{
                                if(res.data.code == 1){
                                    wx.showToast({
                                        title: res.data.msg,
                                        icon: 'success',
                                        duration: 2000
                                    })
                                }else{
                                    wx.showToast({
                                        title: res.data.msg,
                                        icon: 'none',
                                        duration: 2000
                                    })
                                }
                            })
                        } else if (res.cancel) {
                            console.log('用户点击取消')
                        }
                    }
                })
            }
        })
    },
   
    companyHandle() {
        let user = App.globalData.userInfo;
        if (!user) {
            Api.login('/pages/company_info/company_info')
        } else {
            wx.navigateTo({
                url: '/pages/company_info/company_info',
            })
        }
    },

    farmerHandle() {
        let user = App.globalData.userInfo;
        if (!user) {
            Api.login('/pages/company_info/company_info')
        } else {
            wx.navigateTo({
                url: '/pages/farmer_info/farmer_info',
            })
        }
    },
    // 添加员工
    addStaffSubmit: function(e) {
        let _this = this;
        let info = e.detail.value;
        if (!info.wname) {
            wx.showToast({
                title: '请输入姓名',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (!info.wphone) {
            wx.showToast({
                title: '请输入电话',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        let data = {
            'wname': info.wname,
            'wphone': info.wphone,
        };
        Api.fetch('inviteWorker', data)
            .then(res => {
                console.log(res.data);
                if (res.data.code == 1) {
                    _this.setData({
                            staff: false,
                        }),
                        wx.showToast({
                            title: res.data.msg,
                            icon:'none'
                        })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            })
            .catch(e => {
                console.log(e);
            })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    async onPullDownRefresh() {
        var that = this;
        if (that.data.usertype == 2) {

        } else {
            var res = await that.getMyWorkers()
        }
        if (res) {
            wx.stopPullDownRefresh()
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})